// views/index/shop/mailservice/third/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../utils/area');
var util = require('../../../../../utils/util');
import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wenjiandaivalue:0,
    baozhangvalue:0,
    schoollist: '',
    majorlist: '',
    keys: '',
    wenjiandai: false,
    baozhang: false,
    openrefusebagpup: false,
    openbaozhengpup: false,
    showcountry: false,
    showschool: false,
    showmajor: false,
    showstate: false,
    columncountry: [],
    columnschool: ['其它'],
    columnmajor: ['其它'],
    columnstate: ['德国', '奥地利', '瑞士', '比利时'],
    country: '',
    school: '',
    major: '',
    shouname: '',
    phone: '',
    state: '德国',
    shoucountry: '',
    postcode: '',
    dizhi: '',
    beizhu: '',
    email: '',
    price: 235,
    userid: '',
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      userid: userinfo.id,
      id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/liude/upload/xuexiao_api.json`,
      method: "GET",

      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        _this.setData({
          columncountry: Object.keys(res.data),
          schoollist: res.data
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  //下一步按钮
  gofourth: function () {
    var _this = this
    if (!_this.ifenglish(_this.data.shouname)) {
      Notify('姓名输入错误,请使用英文字母填写');
    }

    if (!_this.ifenglish(_this.data.shoucountry)) {
      Notify('城市输入错误,请使用英文字母填写');
    }
    if (!_this.ifenglish(_this.data.dizhi)) {
      Notify('详细地址输入错误,请使用英文字母填写');
    }
    if (_this.data.email == '') {
      Notify('邮箱不能为空');
    }

    if (_this.ifenglish(_this.data.shouname) &&
      _this.ifenglish(_this.data.shoucountry) && _this.ifenglish(_this.data.dizhi) && _this.data.email !== ''
    ) {
      console.log(_this.data.country,_this.data.school,_this.data.major,_this.data.shouname,_this.data.phone,_this.data.state,_this.data.shoucountry,_this.data.postcode,_this.data.dizhi,_this.data.beizhu,_this.data.email,_this.data.wenjiandaivalue,_this.data.baozhangvalue)
      wx.request({
        url: `${BASE_URL}/dhl/mobileinfonext`,
        method: "POST",
        data: {
          zhou: _this.data.country,
          school: _this.data.school,
          major: _this.data.major,
          get_user: _this.data.shouname,
          phone: _this.data.phone,
          country: _this.data.state,
          city: _this.data.shoucountry,
          yb: _this.data.postcode,
          detail_address: _this.data.dizhi,
          content: _this.data.beizhu,
          email: _this.data.email,
          file_dai: _this.data.wenjiandaivalue,
          bao: _this.data.baozhangvalue,
          uid:_this.data.userid,
          id:_this.data.id
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `../fourth/index?id=${res.data}`
          })
        }
      })

    }

  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
















  //打开州选择器
  open_country: function () {
    this.setData({
      showcountry: true,
    })
  },
  //打开学校选择器
  open_school: function () {
    this.setData({
      showschool: true,
    })
  },
  //打开申请专业选择器
  open_major: function () {
    this.setData({
      showmajor: true,
    })
  },
  //打开海外选择器
  open_state: function () {
    this.setData({
      showstate: true,
    })
  },

  //关闭选择器
  onCancel: function () {
    this.setData({
      showcountry: false,
      showschool: false,
      showmajor: false,
      showstate: false,
    })
  },
  //确定内容并关闭选择器
  onConfirmcountry: function (e) {
    // console.log(this.data.list[e.detail.value],[Object.keys(this.data.list[e.detail.value])],e.detail.value)
    var str = ['其它']
    Object.keys(this.data.schoollist[e.detail.value]).forEach(item => {
      str.unshift(this.data.schoollist[e.detail.value][item])
    })
    this.setData({
      showcountry: false,
      country: e.detail.value,
      columnschool: str,
      school: '',
      major: ''
    })
  },
  onConfirmschool: function (e) {
    for (let i in this.data.schoollist[this.data.country]) {
      if (this.data.schoollist[this.data.country][i] == e.detail.value) {
        var key = i
      }
    }
    var _this = this
    wx.request({
      url: `${BASE_URL}/liude/upload/zhuanye_api.json`,
      method: "GET",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        if (e.detail.value == '其它') {
          _this.setData({
            columnmajor: ['其它']
          })
        } else {
          _this.setData({
            columnmajor: [...res.data[key]].concat(['其它'])
          })
        }

      }
    })
    this.setData({
      showschool: false,
      school: e.detail.value,
      major: ''
    })
  },
  onConfirmmajor: function (e) {
    this.setData({
      showmajor: false,
      major: e.detail.value
    })
  },

  onConfirmstate: function (e) {
    console.log(e.detail.value)
    this.setData({
      showstate: false,
      state: e.detail.value
    })
  },































  //切换是否需要垃圾袋
  onChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      wenjiandai: detail
    });
    if (detail) {
      this.setData({
        price: this.data.price + 5,
        wenjiandaivalue:1
      });
    } else {
      this.setData({
        price: this.data.price - 5,
        wenjiandaivalue:0
      });
    }
  },
  //切换是否需要垃圾袋
  onchange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      baozhang: detail
    }); 
    if (detail) {
      this.setData({
        price: this.data.price + 32,
        baozhangvalue:1
      });
    } else {
      this.setData({
        price: this.data.price - 32,
        baozhangvalue:0
      });
    }                
  },
  openrefusebagpup: function () {
    this.setData({
      openrefusebagpup: true
    })
  },
  openbaozhengpup: function () {
    this.setData({
      openbaozhengpup: true
    })
  },
  closefusebagpop: function () {
    this.setData({
      openrefusebagpup: false,
      openbaozhengpup: false
    })
  },




})